#coding:utf8

from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StringType, IntegerType, StructField

# 用sql形式实现单词计数
if __name__ == '__main__':
    # 构建SparkSession执行环境入口对象
    spark = SparkSession.builder.\
        appName("test_parque").\
        master("local[*]").\
        getOrCreate()

    sc = spark.sparkContext

    rdd = sc.textFile("../data/input/words.txt")

    st = StructType([StructField("word", StringType(), True)])

    df = rdd.flatMap(lambda x:x.split(" ")).\
        map(lambda x:[x]).\
            toDF(st)

    df.printSchema()
    df.show()

    df.createTempView("word_table")

    spark.sql("select word,count(1) from word_table group by word").show()

